package eng.agile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

public class TrakerActivity extends MapActivity {
	static String value1;
	static String value2;
	static EditText e1;
	static EditText e2;
	static TextView curText;
	int longit = 0;
	int latit = 0;
	MapView m;
	public MapController mc;
	GeoPoint p;
	Location l ;
	LocationManager locMan;
	LocationListener locLst;
	String towers;
	
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		curLocation();
		getDisplayLocation();

	}
	protected boolean isRouteDisplayed() {
		return false;
	}

	public void getDisplayLocation(){
		findViewById(R.id.button3).setVisibility(4);

		final Builder builder = new AlertDialog.Builder(this);
		final Button route1 = (Button) findViewById(R.id.button2);
		route1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				findViewById(R.id.button3).setVisibility(4);
				e1 = (EditText)findViewById(R.id.editText1);
				e2 = (EditText)findViewById(R.id.editText2);
				value1 = e1.getText().toString();
				value2 = e2.getText().toString();
				if( e2.getText().length()>0){
					if(e1.getText().length()==0){

						//e1.setText("Current Location");
						builder.setMessage("This app will crash SOON because there is no start address")
						.setNegativeButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
								findViewById(R.id.progressBar1).setVisibility(4);
							}
						});
						AlertDialog dialog = builder.create();
						dialog.show();
						
						
						

					}
					builder.setMessage("Route From : "+e1.getText().toString()+"\nto : "+e2.getText().toString())
					.setCancelable(true)
					.setPositiveButton("yes", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							curRoute(value1, value2);
							//	Uri uri = Uri.parse("http://maps.google.com/maps?&saddr=13.042206,80.17000&daddr=9.580000,78.100000");
							//		   Intent i1 = new Intent(Intent.ACTION_VIEW);//TrakerActivity.this, TrakerActivity.class);
							//		   i1.setData(uri);
							//		   startActivity(i1);
							//	   finish();
							findViewById(R.id.button3).setVisibility(1);
							findViewById(R.id.linearLayoutMain).setVisibility(4);
							findViewById(R.id.progressBar1).setVisibility(1);
						}
					})
					.setNegativeButton("No", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
							findViewById(R.id.progressBar1).setVisibility(4);
						}
					});

					AlertDialog dialog = builder.create();
					dialog.show();
				}
				else{
					builder.setMessage("Try to provide Destination Address")
					.setNegativeButton("OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
							findViewById(R.id.progressBar1).setVisibility(4);
						}
					});
					AlertDialog dialog = builder.create();
					dialog.show();
				}
			}
		});

		final Button interChange = (Button) findViewById(R.id.button1);
		interChange.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				e1 = (EditText)findViewById(R.id.editText1);
				e2 = (EditText)findViewById(R.id.editText2);
				findViewById(R.id.progressBar1).setVisibility(4);
				if(e2.getText().length() > 0 ){

					if(e1.getText().length() == 0 ){
						//      e1.setText("Current Location");


					}
					value1 = e1.getText().toString();
					value2 = e2.getText().toString();

					e1.setText(value2);
					e2.setText(value1);
				}
				else
				{
					builder.setMessage("Try to provide Destination Address").setNegativeButton("OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});
					AlertDialog dialog = builder.create();
					dialog.show();
				}
			}
		});
		final Button location = (Button) findViewById(R.id.button3);
		location.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				findViewById(R.id.linearLayoutMain).setVisibility(1);
				findViewById(R.id.progressBar1).setVisibility(4);
				findViewById(R.id.button3).setVisibility(4);

			}
		});
		final Button curPosition = (Button) findViewById(R.id.button4);
		curPosition.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				findViewById(R.id.linearLayoutMain).setVisibility(1);
				findViewById(R.id.progressBar1).setVisibility(4);
				findViewById(R.id.button3).setVisibility(4);
				curLocation();
			}
		});
	}

	public void curLocation(){
		
		m = (MapView) findViewById(R.id.mapView);
		//this.m = m;
		m.setBuiltInZoomControls(true);
		 mc = m.getController();
		//this.mc = mc;
		locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		//this.locMan = locMan;
		mc.setZoom(10); 
		locLst = new MyLocationListener();
		//this.locLst = locLst;
		m.getOverlays();
		locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new GeoLocation() );
		locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locLst );
		m.invalidate();
	}
	
	
	
	
	public void curRoute(String s1, String s2){

		m = (MapView) findViewById(R.id.mapView);
		m.setBuiltInZoomControls(true);
	//	mc = m.getController();
		//locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		//mc.setZoom(10); 
		//locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new GeoLocation());
		//m.invalidate();

		String locationName=s1;
		
		try{
			Geocoder g = new Geocoder(this);;
			List<Address>addressList=g.getFromLocationName(locationName,1);
			if(addressList!=null && addressList.size()>0)
			{
			 int lat=(int)(addressList.get(0).getLatitude()*1000000);
			 int lng=(int)(addressList.get(0).getLongitude()*1000000);

			GeoPoint pt=new GeoPoint(lat,lng);
			m.getController().setZoom(15);
			m.getController().setCenter(pt);
			
			}
		
			}catch(IOException e){
			e.printStackTrace();
			}
		
		String locationName2=s2;
		
		try{
			Geocoder g = new Geocoder(this);;
			List<Address>addressList=g.getFromLocationName(locationName2,5);
			if(addressList!=null && addressList.size()>0)
			{
			 int lat=(int)(addressList.get(1).getLatitude()*1000000);
			 int lng=(int)(addressList.get(1).getLongitude()*1000000);

			GeoPoint pt=new GeoPoint(lat,lng);
			m.getController().setZoom(15);
			m.getController().setCenter(pt);
			mc = m.getController();
			mc.setZoom(10); 
			m.invalidate();
			
			}
		
			}catch(IOException e){
			e.printStackTrace();
			}

		
	}



	public void onLocationChanged(Location location) {
		latit = (int) (location.getLatitude()*1E6);
		longit = (int) (location.getLongitude()*1E6);

	}
	public void onProviderDisabled(String provider) {

	}
	public void onProviderEnabled(String provider) {

	}
	public void onStatusChanged(String provider, int status, Bundle extras) {

	}



	private class GeoLocation extends Activity implements LocationListener {
		public void onLocationChanged(Location location) {
			
			if(location != null)
			{


				latit = (int) (location.getLatitude()*1E6);
				longit = (int) (location.getLongitude()*1E6);
				p = new GeoPoint(latit, longit);
				String address = "NOTHING IS WORKING UNTILL TODAY";//ConvertPointToLocation(point);
				//Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
				//Toast.makeText(getBaseContext(), "Latitude:"+location.getLatitude()+" Longitude: "+location.getLongitude(), Toast.LENGTH_SHORT).show();
				mc.animateTo(p);
				mc.setZoom(10);
				m.invalidate();

			}
		}


		public String ConvertPointToLocation(GeoPoint point) {   
			String address = "No Location Found";
			Geocoder gc = new Geocoder(this, Locale.getDefault());
			try {
				List<Address> a = gc.getFromLocation(
						point.getLatitudeE6()  / 1E6, 
						point.getLongitudeE6() / 1E6, 1);

				if (a.size() > 0) {
					for (int index = 0; 
							index < a.get(0).getMaxAddressLineIndex(); index++)
						address += a.get(0).getAddressLine(index) + " ";
				}
			}
			catch (IOException e) {        
				e.printStackTrace();
			}   

			return address;
		} 

		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
		}

		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
		}
}
	public class MyLocationListener implements LocationListener

	{

	public void onLocationChanged(Location loc)

	{

	loc.getLatitude();

	loc.getLongitude();

	String Text = "My current location is: " +

	"Latitud = "+ loc.getLatitude() +

	"Longitud = " + loc.getLongitude();

	Toast.makeText( getApplicationContext(),

	Text,

	Toast.LENGTH_SHORT).show();

	}



	public void onProviderDisabled(String provider)

	{

	Toast.makeText( getApplicationContext(),

	"Gps Disabled",

	Toast.LENGTH_SHORT ).show();

	}

	public void onProviderEnabled(String provider)

	{

	Toast.makeText( getApplicationContext(),

	"Gps Enabled",

	Toast.LENGTH_SHORT).show();

	}

	

	public void onStatusChanged(String provider, int status, Bundle extras)

	{

	}

	}/* End of Class MyLocationListener */


}
//geo fix 12.169919 2.387917
//geo fix 42.307943 -83.071064